package com.cn.xiaonuo.modular.service.app;

import java.io.File;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cn.xiaonuo.modular.dao.VideoDeployDao;
import com.cn.xiaonuo.modular.dao.VideoStoreDao;
import com.cn.xiaonuo.modular.model.app.VideoDeploy;
import com.cn.xiaonuo.modular.model.app.VideoStore;

@Service
public class VideoDeployAndStoreService {

	@Autowired
	private VideoDeployDao videoDeployDao;
	
	@Autowired
	private VideoStoreDao videoStoreDao;
	
	public VideoDeploy fingVideoDeploy() {
		return videoDeployDao.find();
	}

	public void insertOrUpdateVideoDeploy(VideoDeploy videoDeploy) {
		if(videoDeployDao.findCount()>0) {
			videoDeploy.setUpdateTime(new Date());
			videoDeployDao.updateById(videoDeploy);
		}else {
			videoDeploy.setUpdateTime(new Date());
			videoDeploy.setCreateTime(new Date());
			videoDeployDao.insert(videoDeploy);
		}
	}
	
	public void videoStore(VideoStore videoStore,MultipartFile video,MultipartFile screenshot,HttpServletRequest request,String upload) {
		  String imgPath=null;
		  String imgPath2=null;
			if(!(video.getOriginalFilename().equals("") || video.getOriginalFilename()==null)) {
  			String uploadFileName=UUID.randomUUID()+video.getOriginalFilename().substring(video.getOriginalFilename().lastIndexOf("."));
  			File saveFile=new File(upload+File.separatorChar+uploadFileName);
  			try {
				FileUtils.copyInputStreamToFile(video.getInputStream(), saveFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
  			//imgPath="upload/"+uploadFileName;
  			imgPath=uploadFileName;
  			videoStore.setVideoRoute(imgPath);
  		}	
			if(!(screenshot.getOriginalFilename().equals("") || screenshot.getOriginalFilename()==null)) {
	  			String uploadFileName2=UUID.randomUUID()+screenshot.getOriginalFilename().substring(screenshot.getOriginalFilename().lastIndexOf("."));
	  			File saveFile2=new File(upload+File.separatorChar+uploadFileName2);
	  			try {
					FileUtils.copyInputStreamToFile(video.getInputStream(), saveFile2);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	  			//imgPath="upload/"+uploadFileName;
	  			imgPath2=uploadFileName2;
	  			videoStore.setVideoScreen(imgPath2);
	  		}	
			videoStore.setCreateTime(new Date());
			videoStore.setUpdateTime(new Date());
			videoStoreDao.insert(videoStore);
	}
	public List<VideoStore> findVideoStore(VideoStore videoStore) {
		return videoStoreDao.createLambdaQuery()
				.andEq(VideoStore :: getUserId, videoStore.getUserId())
				.orderBy(VideoStore :: getCreateTime)
				.select();
	}
}